package com.atguigu.gmall.common.utils;

import java.lang.reflect.Field;

public class BeanUtil {

    public static Object injectSameField(Object origin, Object target) {
        Class ct1 = (Class) origin.getClass();
        Class ct2 = (Class) target.getClass();

        Field[] fs = ct1.getDeclaredFields();
        Field[] nfs = ct2.getDeclaredFields();
        for (int i = 0; i < fs.length; i++) {
            Field f = fs[i];
            f.setAccessible(true); //设置些属性是可以访问的
            Object val = new Object();
            try {
                val = f.get(origin);//得到此属性的值
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }
            for (int j = 0; j < nfs.length; j++) {
                Field nf = nfs[j];
                nf.setAccessible(true); //设置些属性是可以访问的
                if (f.getName().equals(nf.getName())) {
                    try {
                        nf.set(target, val);
                    } catch (IllegalAccessException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
        return target;
    }


}
